package cn.always.xiajia.admin.sys.service.impl;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import org.springframework.stereotype.Service;

import cn.always.xiajia.admin.sys.entity.SysUserRole;
import cn.always.xiajia.admin.sys.mapper.SysUserRoleMapper;
import cn.always.xiajia.admin.sys.service.SysUserRoleService;
import cn.always.xiajia.framework.common.util.ExtCollUtil;
import cn.always.xiajia.framework.common.util.ExtCollectionUtils;
import cn.always.xiajia.framework.mybatis.core.service.impl.BaseServiceImplX;

@Service("sysUserRoleService")
public class SysUserRoleServiceImpl extends BaseServiceImplX<SysUserRoleMapper, SysUserRole> implements SysUserRoleService {

	/**
	 * 根据用户获取对应角色Id
	 * 
	 * @param menuId
	 * @return
	 */
	@Override
	public Set<Long> getRoleIdByUserId(Long userId) {
		if (userId == null) {
			return Collections.emptySet();
		}
		Map<String, Object> param = new HashMap<>();
		param.put("EQ_userId", userId);
		return ExtCollectionUtils.convertSet(xjList(param), SysUserRole::getRoleId);
	}

	/**
	 * 获得拥有角色的用户编号集合
	 *
	 * @param roleId 角色编号
	 * @return 用户编号集合
	 */
	@Override
	public Set<Long> getUserIdByRoleId(Long roleId) {
		return getUserIdByRoleIds(Collections.singletonList(roleId));
	}

	/**
	 * 获得拥有多个角色的用户编号集合
	 *
	 * @param roleIds 角色编号集合
	 * @return 用户编号集合
	 */
	@Override
	public Set<Long> getUserIdByRoleIds(Collection<Long> roleIds) {
		if (ExtCollUtil.isEmpty(roleIds)) {
			return Collections.emptySet();
		}
		Map<String, Object> param = new HashMap<>();
		param.put("IN_roleId", roleIds);
		return ExtCollectionUtils.convertSet(xjList(param), SysUserRole::getUserId);
	}
}
